package com.pinyoumei.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.pinyoumei.app.R;
import com.pinyoumei.app.adapter.ChooseCouponAdapter;
import com.pinyoumei.app.bean.ChooseCouponBean;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.pinyoumei.app.utils.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2021-06-26.
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2021-06-26        Administrator             新增Person类
 * 2021-06-26        Administrator             增加sex属性
 * 选择优惠券
 */
public class CouponChooseActivity extends BaseActivity implements View.OnClickListener {
    private int page =1;
    private int coupon_type = 0;
    private String deduct_amount = "";
//    private String goods_id = "";
    private String coupon_id = "";
    private float total_price = 0.00f;
    private ChooseCouponAdapter chooseCouponAdapter;
    private List<ChooseCouponBean.ResultBean.UsableCouponListBean> all_coupon_list = new ArrayList<>();
    private TextView tv_deduct_amount;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupon_choose);

//        coupon_type = getIntent().getIntExtra("coupon_type",0);

        //购买的产品id 境外商城选择优惠券的时候 必传 \
//        goods_id = getIntent().getStringExtra("goods_id");



        InitWidget();
        if (!TextUtils.isEmpty(getIntent().getStringExtra("total_price"))){
            total_price = Float.parseFloat(getIntent().getStringExtra("total_price"));
            InitData(total_price);
        }


//        if (TextUtils.isEmpty(goods_id)){
//            //机具商城
//            total_price = Float.parseFloat(getIntent().getStringExtra("total_price"));
//            InitData("",1,total_price);
//        }else{
//            //境外商城
//            InitData(goods_id,2,0.00f);
//        }


    }


    private void InitWidget() {
        tv_deduct_amount = findViewById(R.id.tv_deduct_amount);
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        RelativeLayout relative_finish = findViewById(R.id.relative_finish);
        relative_finish.setOnClickListener(this);
        Button bt_sure = findViewById(R.id.bt_sure);
        bt_sure.setOnClickListener(this);
        
//        SmartRefreshLayout swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
//        //首页数据下拉刷新监听
//        swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
//
//            @Override
//            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//                page = 1;
//                InitData();
//                refreshLayout.finishRefresh();
//
//            }
//        });
//        swipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
//            @Override
//            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
//                page++;
//                LoadData();
//                refreshLayout.finishLoadMore();
//            }
//        });



        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
        chooseCouponAdapter = new ChooseCouponAdapter(R.layout.item_choose_coupon_list,null);
        recyclerView.setAdapter(chooseCouponAdapter);
        chooseCouponAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < chooseCouponAdapter.getData().size(); i++) {
                    chooseCouponAdapter.getData().get(i).setClick(false);
                }

                if (chooseCouponAdapter.getData().get(position).getType().equals("1")){
                    coupon_id = chooseCouponAdapter.getData().get(position).getId();
                    chooseCouponAdapter.getData().get(position).setClick(true);
                    deduct_amount = chooseCouponAdapter.getData().get(position).getDeduct_amount()+"";
                    String unit_name = chooseCouponAdapter.getData().get(position).getDeduct_amount_unit_name()+"";
                    tv_deduct_amount.setText( deduct_amount + unit_name);
                }


                chooseCouponAdapter.setNewData(chooseCouponAdapter.getData());

                
            }
        });

    }

    private void InitData(float total_price) {

        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(CouponChooseActivity.this));
//        map.put("goods_id", goods_id);
//        map.put("coupon_type", coupon_type);
        map.put("total_price", total_price);


        OkGo.<String>post(Constants.ZBK_COUPON_CHOOSE)
                .params("user_id", SPUtils.getAppUserId(CouponChooseActivity.this))
//                .params("goods_id", goods_id)
//                .params("coupon_type", coupon_type)
                .params("total_price", total_price)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    private List<ChooseCouponBean.ResultBean.UsableCouponListBean> disable_coupon_list;
                    private List<ChooseCouponBean.ResultBean.UsableCouponListBean> usable_coupon_list;

                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);

                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();

                        ChooseCouponBean chooseCouponBean = gson.fromJson(response.body(), ChooseCouponBean.class);
                        if (chooseCouponBean.getCode() >= 0) {
                            //可用优惠券列表
                            usable_coupon_list = chooseCouponBean.getResult().getUsable_coupon_list();
                            if (usable_coupon_list!=null && usable_coupon_list.size()>0){
                                for (int i = 0; i < usable_coupon_list.size(); i++) {
                                    usable_coupon_list.get(i).setClick(false);
                                    usable_coupon_list.get(i).setType("1");
                                }

                                all_coupon_list.addAll(usable_coupon_list);
                            }else{
                                Toast.makeText(CouponChooseActivity.this, "暂无可用的优惠券", Toast.LENGTH_SHORT).show();
                            }
                          

                            //不可用优惠券列表
                            disable_coupon_list = chooseCouponBean.getResult().getDisable_coupon_list();
                            for (int i = 0; i < disable_coupon_list.size(); i++) {
                                disable_coupon_list.get(i).setClick(false);
                                disable_coupon_list.get(i).setType("2");
                            }
                            all_coupon_list.addAll(disable_coupon_list);

                            chooseCouponAdapter.setNewData(all_coupon_list);
                        } else {
                            ToastUtils.showShort(chooseCouponBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });


    }

    private void LoadData() {

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        	case R.id.relative_finish:
        		finish();
        		break;

        		case R.id.bt_sure:
        		//确定
                    if (!TextUtils.isEmpty(deduct_amount)){
                        Intent intent = new Intent();
                        intent.putExtra("coupon_id",coupon_id);
                        intent.putExtra("deduct_amount",deduct_amount);
                        setResult(12000,intent);
                        finish();
                    }

        		break;
        
        	default:
        		break;
        }
    }
}
